﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace AssessmentActions
{
    [Serializable()]
    public class MultipleChoiceQuestion : Question, ISerializable
    {
        private List<string> _answers;
        private int _correctanswer;

        /// <summary>
        /// Constructor to create MultipleChoiceQuestion with all parameters
        /// </summary>
        /// <param name="id">Question ID</param>
        /// <param name="text">Question text</param>
        /// <param name="answers">Array of strings of answers</param>
        /// <param name="answer">Answer as zero based int</param>
        public MultipleChoiceQuestion(int id, string text, List<string> answers, int answer)
        {
            this._id = id;
            this._text = text;
            this._answers = answers;
            this._correctanswer = answer;
        }

        public MultipleChoiceQuestion()
        {
            _answers = new List<string>();
        }

        /// <summary>
        /// Gets the HTML representation of the question
        /// </summary>
        /// <returns>String version of question</returns>
        public override string getQuestionHtml()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("<div class=\"question_multiple_choice\">");
            sb.Append("<span class=\"text\">" + _text + "</span>");
            sb.Append("<ol class=\"choiceList\">");
            foreach (string answer in _answers)
            {
                sb.Append("<li class=\"choice\">" + answer + "</li>");
            }
            sb.Append("</ol>");
            sb.Append("</div>");

            return sb.ToString();
        }

        /// <summary>
        /// Gets the HTML representation of the question with answer
        /// </summary>
        /// <returns>String version of question</returns>
        public override string getQuestionWithAnswerHtml()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("<div class=\"question_multiple_choice\">");
            sb.Append("<span class=\"text\">" + _text + "</span>");
            sb.Append("<ol class=\"choiceList\">");
            for (int i = 0; i < _answers.Count; i++)
            {
                sb.Append("<li class=\"choice");
                if (i == _correctanswer)
                    sb.Append(" answer\">");
                else
                    sb.Append("\">");
                sb.Append(_answers[i]);
                sb.Append("</li>");
            }
            sb.Append("</ol>");
            sb.Append("</div>");

            return sb.ToString();
        }

        public override string getQuestionHtmlWithSelector()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("<div class=\"question_multiple_choice\">");
            sb.Append("<span id=\"questionId\">mc_" + _id + "</span> ");
            sb.Append("<span class=\"text\">" + _text + "</span>");
            sb.Append("<ol class=\"choiceList\">");
            for (int i = 0; i < _answers.Count; i++)
            {
                sb.Append("<li class=\"choice\">");
                sb.Append("<input type=\"radio\" name=\"mc_answers_" + this._id + "\" value=\"" + i + "\" class=\"multipleChoiceRadioButton\" />" + _answers[i]);
                sb.Append("</li>");
            }
            sb.Append("</ol>");
            sb.Append("</div>");

            return sb.ToString();
        }

        /// <summary>
        /// Gets the HTML representation of the question
        /// </summary>
        /// <returns>String version of question</returns>
        public override string getQuestionXml()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("\n\t\t<MultipleChoiceQuestion>");
            sb.Append("\n\t\t\t<text>" + _text + "</text>");
            foreach (string answer in _answers)
            {
                sb.Append("\n\t\t\t<choice>" + answer + "</choice>");
            }
            sb.Append("\n\t\t</MultipleChoiceQuestion>");

            return sb.ToString();
        }

        /// <summary>
        /// Gets the HTML representation of the question with answer
        /// </summary>
        /// <returns>String version of question</returns>
        public override string getQuestionWithAnswerXml()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("\n\t\t<MultipleChoiceQuestion>");
            sb.Append("\n\t\t\t<text>" + _text + "</text>");
            foreach (string answer in _answers)
            {
                sb.Append("\n\t\t\t<choice>" + answer + "</choice>");
            }
            for (int i = 0; i < _answers.Count; i++)
            {
                if (i == _correctanswer)
                {
                    sb.Append("\n\t\t\t<answer>");
                    sb.Append(i);
                    sb.Append("</answer>");
                }
            }
            sb.Append("\n\t\t</MultipleChoiceQuestion>");

            return sb.ToString();
        }

        public void addAnswer(string answerText)
        {
            _answers.Add(answerText);
        }

        public int Correctanswer
        {
            get { return _correctanswer; }
            set { _correctanswer = value; }
        }

        public List<string> Answers
        {
            get { return _answers; }
            set { _answers = value; }
        }

        public override string getCorrectAnswer()
        {
            return _correctanswer.ToString();
        }

        /// <summary>
        /// Deserialization constructor
        /// </summary>
        /// <param name="info"></param>
        /// <param name="ctxt"></param>
        public MultipleChoiceQuestion(SerializationInfo info, StreamingContext ctxt)
        {
            _id = (int)info.GetValue("_id", typeof(int));
            _text = (String)info.GetValue("_text", typeof(String));
            _answers = (List<string>)info.GetValue("_answers", typeof(List<string>));
            _correctanswer = (int)info.GetValue("_correctanswer", typeof(int));
        }

        /// <summary>
        /// Serialization function
        /// </summary>
        /// <param name="info"></param>
        /// <param name="ctxt"></param>
        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            info.AddValue("_id", _id);
            info.AddValue("_text", _text);
            info.AddValue("_answers", _answers);
            info.AddValue("_correctanswer", _correctanswer);
        }
    }

}
